<template>
        <div class="car">
            <div class="inner">
                <img src="@/assets/Shoppingbag.png" alt="" @click="control">
                <div class="price-wrap">
                    <p class="price"><span>￥</span>{{getSum}}</p>
                    <p class="Distribution">另需配送费2.00元</p>
                </div>
                <a href="javascript:;" id="submit" @click="goSettlement">去结算</a>

                <span class="length">{{getLength}}</span>
            </div>
            <transition>
                 <div class="car-top" v-show="isShow">
                    <div class="item"
                    v-for = "item in shoppingCarList" :key="item._id">
                        <img :src="item.src" alt="">
                        <div class="content">
                            <div class="name">{{item.goods_name}}</div>
                            <div class="price"><span>￥</span>{{item.goods_price}}</div>
                        </div>
                        <div class="count">
                            <img src="@/assets/reduce.png" alt="" @click = "reduceGoods(item)">
                            <span>{{item.num}}</span>
                            <img src="@/assets/add.png" alt="" @click = "addGoods(item)">
                        </div>
                    </div>
                </div>
            </transition>
        </div>
</template>

<script>
    export default {
        name:"Car-Component",
        props:['shoppingCarList'],
        data(){
            return {
                isShow:false,
                isSend:false,
                isOpen:false
            }
        },
        computed:{
            // 计算总和
            getSum(){
                let sum = 0
                this.shoppingCarList.forEach(item => {
                    sum = sum + parseFloat(item.goods_price * item.num)
                })
                return sum.toFixed(2)
            },

            // 计算总数量
            getLength(){
                let length = 0
                this.shoppingCarList.forEach(el => {
                    length = length + parseInt(el.num)  
                })
                return length
            },
        },
        methods: {
            control(){
                this.isShow = !this.isShow
                this.isOpen = !this.isOpen
                this.$emit('control',this.isOpen)
            },
            // 结算
            goSettlement(){
               if(this.shoppingCarList.length === 0){
                    this.$store.commit('Home/TIPS','未添加商品，不可结算')
               }else {
                    this.$router.push('/settlement')
               }
               
            },

             // 发送添加到用户购物车的请求
            addGoods(item){
                if(this.isSend === false){
                    this.$store.dispatch('Home/addGoods',item)
                    this.isSend = true
                    setTimeout(()=>{
                        this.$store.dispatch('Home/searchGoodsIntheCar')
                    },40)
                    setTimeout(()=>{
                        this.isSend = false
                    },200)
                }
            },

            // 删除购物车商品
            reduceGoods(item){
                if(this.isSend === false){
                    this.$store.dispatch('Home/reduceGoods',item)
                    this.isSend = true
                    setTimeout(()=>{
                        this.$store.dispatch('Home/searchGoodsIntheCar')
                    },40)
                    setTimeout(()=>{
                        this.isSend = false
                    },200)
                }
            }
        },
    }
</script>

<style scoped lang="less">

    .v-enter,.v-leave-to{
        opacity: 0;
    }

    .v-enter-to,.v-leave{
        opacity: 1;
    }

    .car-top{
        width: 100%;
        height: 64vw;
        bottom: 13.3vw;
        position: absolute;
        z-index:1000;
        background: #fff;
        overflow-y: auto;
        border-radius: 2.7vw;
        box-shadow: 0vw -2.7vw 2.1vw 0vw rgba(0, 0, 0, 0.144);
        transition: .3s;

        .item{
            padding: 2.7vw;
            display: flex;
            height: 13.3vw;
            border-bottom: 0.3vw solid rgb(226, 225, 225);
            img{
                height: 13.3vw;
                width:16vw;                
            }

            .content{
                width: 60%;

                .name{
                    font-size: 4.3vw;
                    font-weight: 600;
                }
                .price{
                    color: red;
                    font-size: 4.3vw;
                    margin-top: 1.3vw;
                    
                    span{
                        font-size: 3.2vw;
                        margin-right: 1.1vw;
                    }
                }
            }

            .count{
                display: flex;
                height: 100%;
                align-items: center;

                img{
                    width: 5.3vw;
                    height: 5.3vw;
                }

                span{
                    font-size: 4.3vw;
                    margin: 0 2.7vw;
                }
            }
        }
    }

    .car{
        width: 100%;
        height: 13.3vw;
        background: #fff;
        position: fixed;
        bottom: 14.6vw;

        .inner{
            width: 95%;
            height: 13.3vw;
            margin: 0 auto;
            display: flex;
            align-items: center;
            background: #fff;
            justify-content: space-between;
            position: relative;
        }

        img{
            width: 9.3vw;
        }

        .price-wrap{
            width: 60%;

            .price{
                color: #333;
                font-weight: 1000;
                font-size: 3.7vw;
            }

            .Distribution{
                font-size: 10.1px;
                color: rgb(155, 153, 153);
            }

        }

        #submit{
            width: 21.3vw;
            text-align: center;
            background: #0965FE;
            height: 9.6vw;
            border-radius: 4.8vw;
            line-height: 9.6vw;
            color: #fff !important;
            text-decoration: none;
        }

        .length{
            width: 5.3vw;
            height: 5.3vw;
            border-radius: 50%;
            background: rgb(221, 35, 35);
            text-align: center;
            color: #fff;
            line-height: 5.3vw;
            position: absolute;
            left: 5.3vw;
            top: 0vw;
        }
    }
</style>